<?php

/**
 * API for common admin WebME functions
 *
 * PHP version 5.2
 *
 * @category None
 * @package  None
 * @author   Kae Verens <kae@kvsites.ie>
 * @license  GPL 2.0
 * @link     http://kvsites.ie/
 */


/*
 * account manager functions
 */
function Core_adminUserDetails() {
	$administrator = Administrator::getInstance();
	return $administrator->userDetails($_REQUEST['id']);
}

function Core_adminAssignedRoles() {
	$administrator = Administrator::getInstance();
	return $administrator->assignedRoles($_REQUEST['id']);
}

function Core_adminAssignedUsers() {
	$administrator = Administrator::getInstance();
	return $administrator->assignedUsers($_REQUEST['id']);
}

function Core_adminDeassignedUsers() {
	$administrator = Administrator::getInstance();
	return $administrator->deassignedUsers($_REQUEST['id']);
}

function Core_adminAssignUser (){
	$administrator = Administrator::getInstance();
	return $administrator->assignUser($_REQUEST['uid'], $_REQUEST['rid']);
}

function Core_adminDeassignUser (){
	$administrator = Administrator::getInstance();
	return $administrator->deassignUser($_REQUEST['uid'], $_REQUEST['rid']);
}

function Core_adminAssignedPermissions (){
	$administrator = Administrator::getInstance();
	return $administrator->assignedPermissions($_REQUEST['id']);
}

function Core_adminDeassignedPermissions (){
	$administrator = Administrator::getInstance();
	return $administrator->deassignedPermissions($_REQUEST['id']);
}

function Core_adminGrantPermission (){
	$administrator = Administrator::getInstance();
	return $administrator->grantPermission($_REQUEST['pid'], $_REQUEST['rid']);
}

function Core_adminRevokePermission (){
	$administrator = Administrator::getInstance();
	return $administrator->revokePermission($_REQUEST['pid'], $_REQUEST['rid']);
}

function Core_adminAddRole (){
	$administrator = Administrator::getInstance();
	$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
	return $administrator->addRole($name);
}

function Core_adminEditRole(){
	$administrator = Administrator::getInstance();
	$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
	return $administrator->editRole($_REQUEST['id'], $name);
}

function Core_adminDeleteRole(){
	$administrator = Administrator::getInstance();
	return $administrator->deleteRole($_REQUEST['id']);
}

function Core_adminDeleteUser(){
	$administrator = Administrator::getInstance();
	return $administrator->deleteUser($_REQUEST['id']);
}

function Core_adminAddUser(){
	$administrator = Administrator::getInstance();
	$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
	$surname = filter_input(INPUT_GET, 'surname', FILTER_SANITIZE_SPECIAL_CHARS);
	$email = filter_input(INPUT_GET, 'email', FILTER_SANITIZE_EMAIL);	
	return $administrator->addUser($name, $surname, $email, $_REQUEST['active'], $_REQUEST['password']);
}

function Core_adminSaveUserDetails(){
	$administrator = Administrator::getInstance();
	$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
	$surname = filter_input(INPUT_GET, 'surname', FILTER_SANITIZE_SPECIAL_CHARS);
	$email = filter_input(INPUT_GET, 'email', FILTER_SANITIZE_EMAIL);
	return $administrator->saveUserDetails($_REQUEST['id'], $name, $surname, $email, $_REQUEST['active'], $_REQUEST['password']);
}


/*
 * page functions
 */

function Core_adminPageChildnodes() {
	$db = Database::getInstance();
	$pid = (int) preg_replace('/[^0-9]/', '', $_REQUEST['id']);
	$c = Core_cacheLoad('pages', 'adminmenu' . $pid);
	if ($c) {
		return $c;
	}
	$rs = $db->dbAll(
			'select id,id as pid,special&2 as hide,type,alias,'
			. '(select count(id) from pages where parent=pid) as children '
			. 'from pages where parent=' . $pid . ' order by ord,name'
	);
	$data = array();
	foreach ($rs as $r) {
		$item = array(
			'data' => $r['alias'],
			'attr' => array(
				'id' => 'page_' . $r['id']
			),
			'children' => $r['children'] ? array() : false
		);
		if ($r['type'] !== '0') {
			$item['attr']['type'] = $r['type'];
		}
		if ($r['hide'] == '2') {
			$item['attr']['hide'] = 'yes';
		}
		$data[] = $item;
	}
	Core_cacheSave('pages', 'adminmenu' . $pid, $data);
	return $data;
}

function Core_adminPageParentsList() {
	$db = Database::getInstance();
	$id = isset($_REQUEST['other_GET_params']) ? (int) $_REQUEST['other_GET_params'] : -1;

	function selectkiddies($i=0, $n=1, $id=0) {
		$arr = array();
		$q = $db->dbAll(
				'select name,id,alias from pages where parent="' . $i . '" and id!="' . $id
				. '" order by ord,name'
		);
		if (count($q) < 1) {
			return $arr;
		}
		foreach ($q as $r) {
			if ($r['id'] != '') {
				$arr[$r['id']] = str_repeat('» ', $n) . $r['name'];
				$arr = array_merge($arr, selectkiddies($r['id'], $n + 1, $id));
			}
		}
		return $arr;
	}
	return array_merge(	array('0' => ' -- none -- '), selectkiddies(0, 0, $id) );
}

function Core_adminPageTypesList() {
	$arr = array();
	global $pagetypes, $PLUGINS;
	foreach ($pagetypes as $a) {
		$arr[$a[0]] = $a[1];
	}
	foreach ($PLUGINS as $n => $p) {
		if (isset($p['admin']['page_type'])) {
			if (is_array($p['admin']['page_type'])) {
				foreach ($p['admin']['page_type'] as $name => $type) {
					$arr[$n . '|' . $name] = $name;
				}
			} else {
				$arr[$n . '|' . $n] = $n;
			}
		}
	}
	return $arr;
}

/**
 * create a copy of a page
 *
 * @return array status of the copy
 */
function Core_adminPageCopy() {
	$db = Database::getInstance();
	$id = (int) $_REQUEST['id'];
	if (!$id) {
		return array('error' => 'no ID provided');
	}
	$p = $db->dbRow('select * from pages where id=' . $id);
	$name = $p['name'];
	$parts = array();
	foreach ($p as $k => $v) {
		if ($k == 'id') {
			continue;
		}
		$parts[] = $k . '="' . addslashes($v) . '"';
	}
	$db->dbQuery('insert into pages set ' . join(',', $parts));
	$id = $db->dbLastInsertId();
	$db->dbQuery('update pages set name="' . addslashes($name) . '_' . $id . '" where id=' . $id);
	Core_cacheClear();
	return array('name' => $name . '_' . $id, 'id' => $id, 'pid' => $p['parent']);
}

/**
 * delete a page
 *
 * @return array status of the deletion
 */
function Core_adminPageDelete() {
	$db = Database::getInstance();
	$id = (int) $_REQUEST['id'];
	if (!$id) {
		return array('error' => 'no ID provided');
	}
	$r = $db->dbRow("SELECT COUNT(id) AS pagecount FROM pages");
	if ($r['pagecount'] < 2) {
		return array('error' => 'there must always be at least one page.');
	}
	$q = $db->dbQuery('select parent from pages where id="' . $id . '"');
	if ($q->rowCount()) {
		$r = $db->dbRow('select parent from pages where id="' . $id . '"');
		$db->dbQuery('delete from page_vars where page_id="' . $id . '"');
		$db->dbQuery('delete from pages where id="' . $id . '"');
		$db->dbQuery(
				'update pages set parent="' . $r['parent'] . '" where parent="' . $id . '"'
		);
		Core_cacheClear();
		$db->dbQuery('update page_summaries set rss=""');
		return array('ok' => 1);
	}
	return array('error' => 'page does not exist');
}

/**
 * move a page
 *
 * @return array status of the move
 */
function Core_adminPageMove() {
	$db = Database::getInstance();
	$id = (int) $_REQUEST['id'];
	$to = (int) $_REQUEST['parent_id'];
	$order = $_REQUEST['order'];
	$db->dbQuery("update pages set parent=$to where id=$id");
	for ($i = 0; $i < count($order); ++$i) {
		$pid = (int) $order[$i];
		$db->dbQuery("update pages set ord=$i where id=$pid");
	}
	Core_cacheClear();
	$db->dbQuery('update page_summaries set rss=""');
	return array('ok' => 1);
}


function Core_adminGetPageData(){
	$id = $_REQUEST['id'];
	//$db = Database::getInstance();
	//$page = $db->dbRow('select * from pages where id=' . $_REQUEST['id']);
	$page = Page::getInstance($id);
	return $page;
}


















/**
 * save a session variable
 *
 * @return array status of save
 */
function Core_adminSaveJSVar() {
	if (!isset($_SESSION['js'])) {
		$_SESSION['js'] = array();
	}
	foreach ($_REQUEST as $k => $v) {
		if (in_array($k, array('a', 'p', 'f', '_remainder'))) {
			continue;
		}
		$_SESSION['js'][$k] = $v;
	}
	return array('ok' => 1);
}

/**
 * save a session variable
 *
 * @return array status of save
 */
function Core_adminLoadJSVars() {
	if (!isset($_SESSION['js'])) {
		$_SESSION['js'] = array();
	}
	return $_SESSION['js'];
}
